import 'dart:math';
import 'package:aichat_flutter_wallet/module/module_dapp/widgets/hover_status.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/cupertino.dart';
import 'bottom_sheet_widget.dart';

class NumericKeyboardController {
  final List<int> keyboard = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> result = [];
  Function(String)? inputCallback;

  NumericKeyboardController({this.inputCallback});

  random() {
    keyboard.shuffle(Random());
  }

  addNumber(value) {
    result.add(value);
    notifyChange();
  }

  removeNumber() {
    if (result.isNotEmpty) {
      result.removeAt(result.length - 1);
      notifyChange();
    }
  }

  notifyChange() {
    if (null != inputCallback) {
      inputCallback!.call(result.join());
    }
  }
}

class RandomNumericKeyboard extends StatelessWidget {
  final NumericKeyboardController controller;

  const RandomNumericKeyboard({super.key, required this.controller});

  Widget _numWidget(int index) {
    int num = controller.keyboard[index];
    return Expanded(
        child: HoverStatus(
      defaultColor: Colors.transparent,
      hoverColor: AppColors.c_F6F6F6,
      radius: 10.w,
      onPressed: () {
        controller.addNumber(num);
      },
      child: Container(
        alignment: Alignment.center,
        width: 100.w,
        height: 50.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.w),
        ),
        child: Text(
          num.toString(),
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 23.sp, fontWeight: FontWeight.bold, fontFamily: '_DIN', color: AppColors.c_333333),
        ),
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        children: [
          SizedBox(
            height: 15.w,
          ),
          SizedBox(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [_numWidget(0), _numWidget(1), _numWidget(2)],
            ),
          ),
          SizedBox(
            height: 15.w,
          ),
          SizedBox(
            child: Row(
              children: [_numWidget(3), _numWidget(4), _numWidget(5)],
            ),
          ),
          SizedBox(
            height: 15.w,
          ),
          SizedBox(
            child: Row(
              children: [_numWidget(6), _numWidget(7), _numWidget(8)],
            ),
          ),
          SizedBox(
            height: 15.w,
          ),
          SizedBox(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const Expanded(child: SizedBox()),
                _numWidget(9),
                Expanded(
                    child: Center(
                  child: Container(
                    child: HoverStatus(
                      defaultColor: Colors.transparent,
                      hoverColor: AppColors.c_F6F6F6,
                      onPressed: () {
                        controller.removeNumber();
                      },
                      radius: 10,
                      child: Container(
                        alignment: Alignment.center,
                        width: 100.w,
                        height: 50.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10.w),
                        ),
                        child: Image(
                          image: AssetImage('assets/images/wallet/dialog-close.png'),
                          width: 28.w,
                          height: 24.w,
                        ),
                      ),
                    ),
                  ),
                ))
              ],
            ),
          ),
          SizedBox(
            height: 15.w,
          )
        ],
      ),
    );
  }
}

class PasswordAuthDialog extends StatefulWidget {
  final String? title;

  const PasswordAuthDialog({super.key, this.title});

  @override
  State<StatefulWidget> createState() {
    return _PasswordAuthDialogState();
  }
}

class _PasswordAuthDialogState extends State<PasswordAuthDialog> {
  String inputResult = '';
  late NumericKeyboardController _keyboardController;

  @override
  void initState() {
    super.initState();

    _keyboardController = NumericKeyboardController(inputCallback: (input) {
      if (input.length == 8) {
        Navigator.of(context).pop(input);
        return;
      }
      setState(() {
        inputResult = input;
      });
    });

    _keyboardController.random();
  }

  _dotWidget(bool fill) {
    Widget blackFill = UnconstrainedBox(
      child: SizedBox(
        width: 10.w,
        height: 10.w,
        child: DecoratedBox(
            decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: Colors.black,
        )),
      ),
    );
    return Container(
      height: 12.w,
      width: 12.w,
      margin: EdgeInsets.only(right: 15.w),
      decoration: BoxDecoration(
          color: fill ? AppColors.c_333333 : Colors.transparent,
          borderRadius: BorderRadius.circular(6.w),
          border: Border.all(color: AppColors.c_E0E0E0, width: fill ? 0 : 1)),
      child: fill ? blackFill : null,
    );
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
        title: widget.title ?? 'Authentication',
        expand: false,
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 30.w, bottom: 30.w),
              decoration: BoxDecoration(color: AppColors.c_white),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  _dotWidget(inputResult.isNotEmpty),
                  _dotWidget(inputResult.length > 1),
                  _dotWidget(inputResult.length > 2),
                  _dotWidget(inputResult.length > 3),
                  _dotWidget(inputResult.length > 4),
                  _dotWidget(inputResult.length > 5),
                  _dotWidget(inputResult.length > 6),
                  _dotWidget(inputResult.length > 7),
                ],
              ),
            ),
            RandomNumericKeyboard(controller: _keyboardController),
            SizedBox(
              height: MediaQuery.of(context).padding.bottom + 10.w,
            )
          ],
        ));
  }
}
